package com.zlc.platform.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zlc.common.core.sys.mapper.MayanUserDataMapper;
import com.zlc.common.core.sys.model.entity.MayanUserData;
import com.zlc.common.core.sys.model.vo.MayanUserDataVo;
import com.zlc.common.core.sys.service.MayanUserDataService;
import com.zlc.common.util.IpUtils;
import com.zlc.common.util.UserHelper;
import com.zlc.platform.service.UserDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class UserDataServiceImpl implements UserDataService {

    @Autowired
    private MayanUserDataService mayanUserDataService;
    @Autowired
    private MayanUserDataMapper mayanUserDataMapper;

    @Override
    public void add(MayanUserDataVo userData) {
        userData.setUserAccount(UserHelper.getNowAccount());
        MayanUserDataVo dbUserData = getUserData(userData);
        if(!ObjectUtil.isEmpty(dbUserData)){
            dbUserData.setContent(userData.getContent());
            dbUserData.setData1(userData.getData1());
            dbUserData.setData2(userData.getData2());
            mayanUserDataService.saveVo(dbUserData);
        }else {
            userData.setId(IdUtil.fastUUID());
            mayanUserDataService.saveVo(userData);
        }
    }

    @Override
    public MayanUserDataVo getUserData(MayanUserDataVo userData) {
        LambdaQueryWrapper<MayanUserData> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MayanUserData::getUserAccount, UserHelper.getNowAccount());
        if(!ObjectUtil.isEmpty(userData.getDataCode())){
            wrapper.eq(MayanUserData::getDataCode, userData.getDataCode());
        }
        if(!ObjectUtil.isEmpty(userData.getDataType())){
            wrapper.eq(MayanUserData::getDataType, userData.getDataType());
        }
        return mayanUserDataService.queryOne(wrapper);
    }
}
